<template>
  <div class="register">
    <!-- 直线 -->
    <div class="register-line"></div>
    <!-- 注册中 -->
    <el-card shadow="hover" v-if="successRegister" :body-style="{border: '1px solid #BF5F09',padding: 0}">
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" label-width="110px" label-position="left">
        <h3 class="title">注册</h3>
        <el-form-item prop="phonenumber" label="手机账号：">
          <el-input :maxlength="11" size="small" style="width:250px" v-model="registerForm.phonenumber" type="text" auto-complete="new-password" placeholder="手机号" @input="handlePhoneInput">
          </el-input>
        </el-form-item>
        <el-form-item prop="code" label="手机验证码：">
          <el-input
            size="small"
            type="text"
            v-model="registerForm.code"
            placeholder="验证码"
            style="width:80px"
            auto-complete="new-password"
            @keyup.enter.native="handleRegister"
          >
          </el-input>
          <el-button
            size="small"
            style="margin-left:10px"
            :disabled="!canSendSms || isDisabled"
            @click="sendMessage"
          >{{ getSms }}</el-button>
        </el-form-item>
        <el-form-item prop="password" label="密码：">
          <el-input
            size="small"
            style="width:250px"
            v-model="registerForm.password"
            type="password"
            auto-complete="new-password"
            placeholder="密码"
            @keyup.enter.native="handleRegister"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword" label="确认密码：">
          <el-input
            size="small"
            style="width:250px"
            v-model="registerForm.confirmPassword"
            type="password"
            auto-complete="new-password"
            placeholder="确认密码"
            @keyup.enter.native="handleRegister"
          >
          </el-input>
          <div>
             密码5~16位，数字、字母、数字/字母+字符
          </div>
        </el-form-item>
        <div>
          <el-form-item label="" label-width="0">
            <el-button
              :loading="loading"
              size="small"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleRegister"
            >
              <span v-if="!loading">注 册</span>
              <span v-else>注 册 中...</span>
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-card>
    <!-- 注册成功 -->
    <el-card shadow="hover" v-else :body-style="{border: '1px solid #BF5F09',padding: 0}">
      <el-form ref="registerForm2" :model="registerForm" :rules="registerRules" class="register-form register-success" label-width="100px" label-position="left">
          <div class="text-center" style="color:#1D8214">√注册成功</div>
          <el-form-item label="服务商ID：">001</el-form-item>
          <el-form-item label="注册手机号：">18955568885</el-form-item>
          <el-form-item label="注册时间：">20221222 20：25：30</el-form-item>
      </el-form>
    </el-card>
    <!--  底部  -->
    <div class="el-register-footer">
      <el-link type="primary" style="color:#1C42C8" :underline="false" @click="$router.push('/login')">返回登陆></el-link>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register,regSendSms,checkRegister } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("密码输入不一致或密码输入格式有误"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        phonenumber: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: "",
        isRegistered: undefined,
      },
      registerRules: {
        phonenumber: [
          { required: true, trigger: "blur", message: "请输入手机号" },
          {
            pattern: /^1[3-9]\d{9}$/, message: '手机号输入格式有误', trigger: 'change'
          }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" },
          { min: 5, max: 16, message: '密码5~16位，数字、字母、数字/字母+字符', trigger: 'change' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入密码" },
          { required: true, validator: equalToPassword,trigger: "change" }
        ],
        code: [
          { required: true, message: "请输入验证码" },
          {
            pattern: /^\d{5}$/, message: '验证码输入有误', trigger: 'change'
          }
        ]
      },
      loading: false,
      captchaEnabled: true,
      canSendSms: false,
      timer: null,
      getSms: '获取',
      count: 60,
      isDisabled: false,
      successRegister: true
    };
  },
  created() {
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            // 注册成功
            this.successRegister = true
          }).catch(() => {
            this.loading = false;
          })
        }
      });
    },
    handlePhoneInput(val) {
      this.$refs.registerForm.validateField('phonenumber', (msg) => {
        console.log(msg)
        if (!msg) {
          checkRegister({ phonenumber: this.registerForm.phonenumber }).then(
              (res) => {
                this.registerForm.isRegistered = res.data === 0
                this.canSendSms = true
              }
          )
        } else {
          this.registerForm.isRegistered = undefined
          this.canSendSms = false
        }
      })
    },
    sendMessage() {
      if (!this.timer) {
        this.isDisabled = true
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= 60) {
            this.count--
            if (this.count !== 0) {
              this.getSms = `${this.count}s`
            } else {
              clearInterval(this.timer)
              this.getSms = '获取'
              this.count = 60
              this.timer = null
              this.isDisabled = false
            }
          }
        }, 1000)
      }
      regSendSms({ phonenumber: this.registerForm.phonenumber }).then((res) => {
        if (res.code === 200 && res.data === true) {
          this.$message.success(res.msg)
        }
      })
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
}
.register-line {
  padding:0px;
  position:absolute;
  border-top:4px solid #1B73B3;
  top:0px;
  width:100%;
  height:3px;
}
.register-form {
  border-radius: 6px;
  background: #ffffff;
  padding: 25px 25px 5px 25px;
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-success {
  .el-form-item {
    margin-bottom: 0!important;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 40px;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
